package cinema.Controller;

import cinema.Dao.RoomDAO;
import cinema.Dao.RowDAO;
import cinema.Entity.Room;
import cinema.Entity.Row;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
@Named
@ViewScoped
public class RoomController implements Serializable {

    @Inject
    private RoomDAO roomDAO;
    @Inject
    private RowDAO rowDAO;

    private String name;
    private Room room;

    private List<Boolean> canEdit;
    private List<Room> rooms;
    private List<SelectItem> rowValues;
    private List<SelectItem> seatValues;

    private String selectedRow;
    private String selectedSeat;

    @PostConstruct
    public void init() {
        rooms = roomDAO.findAll();
        canEdit = new ArrayList<Boolean>();
        for (int i = 0; i < rooms.size(); ++i) canEdit.add(false);

        rowValues = new ArrayList<SelectItem>();
        for (int i = 1; i < 16; ++i)
            rowValues.add(new SelectItem("" + i, "" + i));
        selectedRow = "1";

        seatValues = new ArrayList<SelectItem>();
        for (int i = 1; i < 16; ++i)
            seatValues.add(new SelectItem("" + i, "" + i));
        selectedSeat = "1";
    }

    public void addRoom() {
        Room room = new Room(name);
        rooms.add(room);
        canEdit.add(false);
        roomDAO.save(room);
        for (int i = 1; i <= Integer.parseInt(selectedRow); ++i) {
            Row row = new Row(room, i, Integer.parseInt(selectedSeat));
            rowDAO.save(row);
        }
    }

    public void deleteRoom() {
        try {
            roomDAO.delete(room);
            canEdit.remove(rooms.indexOf(room));
            rooms.remove(room);
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage("movieTable",
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "cannot delete room if showing is registered for it", "cannot delete room if showing is registered for it"));
        }
    }

    public void editRoom() {
        canEdit.set(rooms.indexOf(room), true);
    }

    public void saveRoom() {
        canEdit.set(rooms.indexOf(room), false);
        roomDAO.update(room);
    }

    public String getName() {
        return name;
    }

    //setname
    public void setName(String name) {
        this.name = name;
    }

    public Room getRoom() {
        return room;
    }

    public void setRoom(Room room) {
        this.room = room;
    }

    public List<Room> getRooms() {
        return rooms;
    }

    public List<Boolean> getCanEdit() {
        return canEdit;
    }

    public void setCanEdit(List<Boolean> canEdit) {
        this.canEdit = canEdit;
    }

    public List<SelectItem> getRowValues() {
        return rowValues;
    }

    public List<SelectItem> getSeatValues() {
        return seatValues;
    }

    public String getSelectedRow() {
        return selectedRow;
    }

    public void setSelectedRow(String selectedRow) {
        this.selectedRow = selectedRow;
    }

    public String getSelectedSeat() {
        return selectedSeat;
    }

    public void setSelectedSeat(String selectedSeat) {
        this.selectedSeat = selectedSeat;
    }
}